//
//  VISTACustomTabBar.m
//  VISTA看天下
//
//  Created by mac12 on 15/11/29.
//  Copyright (c) 2015年 mac12. All rights reserved.
//

#import "VISTACustomTabBar.h"

@implementation VISTACustomTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        NSArray *selectedImage   = @[@"资讯_2.png",@"杂志_2.png",@"微言_2.png",@"酷图_2.png"];
        NSArray *unSelectedImage = @[@"资讯_1.png",@"杂志_1.png",@"微言_1.png",@"酷图_1.png"];
        self.selectedImageArray   = [NSArray arrayWithArray:selectedImage];
        self.unSelectedImageArray = [NSArray arrayWithArray:unSelectedImage];
        self.btnArray = [[NSMutableArray alloc]init];
        float width = frame.size.width/self.selectedImageArray.count;
        for (int i = 0; i < self.selectedImageArray.count; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(width * i, 0, width, frame.size.height);
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.unSelectedImageArray[i]]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [_btnArray addObject:btn];
            [self addSubview:btn];
        }
    }
    [self updateStates:0];
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame WithSelectedImage:(NSArray *)selectedImg WithUnSelectedImage:(NSArray *)unSelectedImg
{
    if (self = [super initWithFrame:frame]) {
        self.selectedImageArray   = [NSArray arrayWithArray:selectedImg];
        self.unSelectedImageArray = [NSArray arrayWithArray:unSelectedImg];
        self.btnArray = [[NSMutableArray alloc]init];
        float width = frame.size.width/self.selectedImageArray.count;
        for (int i = 0; i < self.selectedImageArray.count; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(width * i, 0, width, frame.size.height);
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.unSelectedImageArray[i]]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [_btnArray addObject:btn];
            [self addSubview:btn];
        }
    }
    [self updateStates:0];
    return self;
}
- (void)click:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    NSUInteger index = [_btnArray indexOfObject:btn];
    if ([self.delegate respondsToSelector:@selector(customTabBar:didClickButton:WithButtonIndex:)]) {
        [self.delegate customTabBar:self didClickButton:btn WithButtonIndex:index];
    }
    [self updateStates:index];
}
- (void)updateStates:(NSUInteger)index
{
    for (int i = 0; i < _btnArray.count; i ++) {
        UIButton *btn = [_btnArray objectAtIndex:i];
        if (i == index) {
            //NSLog(@"设置按钮%d的图片为 %@",i,self.selectedImageArray[i]);
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",_selectedImageArray[i]]] forState:UIControlStateNormal];
        }else{
            //NSLog(@"设置按钮%d的图片为 %@",i,self.unSelectedImageArray[i]);
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",_unSelectedImageArray[i]]] forState:UIControlStateNormal];
        }
    }
}


@end
